package com.cjm.one.goods.apps;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjm.one.common.exception.GlobalException;
import com.cjm.one.common.result.Result;
import com.cjm.one.common.result.ResultCodeEnum;
import com.cjm.one.common.utils.AuthCustomerContextHolder;
import com.cjm.one.goods.service.GoodsBrowseHistoryService;
import com.cjm.one.goods.service.GoodsSkuService;
import com.cjm.one.model.goods.dto.StockDeductionDTO;
import com.cjm.one.model.goods.dto.StockRevertDTO;
import com.cjm.one.model.goods.entity.GoodsBrowseHistory;
import com.cjm.one.model.goods.entity.GoodsSku;
import com.cjm.one.model.goods.enums.SortStrategyEnum;
import com.cjm.one.security.annotation.Within;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/app/goods/sku")
@Tag(name = "商品sku", description = "商品sku")
public class AppGoodsSkuController {

    @Resource
    private GoodsSkuService goodsSkuService;
    @GetMapping("/list")
    @Operation(summary = "分页查询+推荐算法")
    @SentinelResource(value = "pageList",blockHandler = "handlerBlockHandler")
    public Result<Page<GoodsSku>> pageList(@RequestParam(name = "current",defaultValue = "1") Long current,
                                           @RequestParam(name = "size",defaultValue = "10") Long size,
                                           @RequestParam(name = "categoryId", required = false) Long categoryId,
                                           @RequestParam(name = "name", required = false) String name,
                                           @RequestParam(name = "SortStrategy", required = false) SortStrategyEnum strategyEnum) {
        Page<GoodsSku> page =  goodsSkuService.pageList(current, size, categoryId, name, strategyEnum);

      return   Result.ok(page);
    }
    public Result<Page<GoodsSku>> handlerBlockHandler(@RequestParam(name = "current",defaultValue = "1") Long current,
                                           @RequestParam(name = "size",defaultValue = "10") Long size,
                                           @RequestParam(name = "categoryId", required = false) Long categoryId,
                                           @RequestParam(name = "name", required = false) String name,
                                           @RequestParam(name = "SortStrategy", required = false) SortStrategyEnum strategyEnum) {
        throw new GlobalException(ResultCodeEnum.SERVICE_UNAVAILABLE);
    }

    @GetMapping("/ByCategoryId/list")
    @Operation(summary = "根据分类查询")
    public Result<Page<GoodsSku>> pageListByCategoryId(@RequestParam(name = "categoryId", required = true) Long categoryId,
                                                       @RequestParam(name = "current",defaultValue = "1") Long current,
                                                       @RequestParam(name = "size",defaultValue = "10") Long size) {
        Page<GoodsSku> page =  goodsSkuService.pageListByCategoryId(categoryId, current
                , size);

        return   Result.ok(page);
    }


    @GetMapping("/history/list")
    @Operation(summary = "查询当前用户浏览历史")
    public Result<Page<GoodsSku>> list(@RequestParam(name="current",defaultValue = "1") Integer  current, @RequestParam(name="size",defaultValue = "10") Integer size) {

        Long id = AuthCustomerContextHolder.getAuthContextCustomer().getId();

        return Result.ok(goodsSkuService.getByUserBrowseHistory(current,size, id));
    }

    @GetMapping("/coupon/list")
    @Operation(summary = "获取限定优惠商品")
    public Result<Page<GoodsSku>> getSkuCoupon(@RequestParam("ids") Long[] ids) {
        return  Result.ok(goodsSkuService.getSkuCouponList(ids));
    }
    /**
     * @description: 扣减库存
     * @author: cjm 
     * @date: 2025/3/30 15:09
     * @param: [ids]
     * @return: com.cjm.one.common.result.Result<java.lang.Boolean>
     **/
    @PostMapping("/deduction/stock")
    @Operation(summary = "扣减库存")
    @Within
    public Result<Boolean> deductionStock(@RequestBody List<StockDeductionDTO> deductionList) {
        return  Result.ok(goodsSkuService.deductionStock(deductionList));
    }
    /**
     * @description: 回滚库存
     * @author: cjm
     * @date: 2025/3/30 16:48
     * @param: [ids]
     * @return: com.cjm.one.common.result.Result<java.lang.Boolean>
     **/
    @PostMapping("/revert/stock")
    @Operation(summary = "回滚库存")
    @Within
    public Result<Boolean> revertStock(@RequestBody List<StockRevertDTO> stockRevertDTOS) {
        return  Result.ok(goodsSkuService.revertStock(stockRevertDTOS));
    }
}
